module CSRhandler (
  input  [31:0]     EXE_inst,
  input  [63:0]     csr_val_exe,
  input  [63:0]     EXE_alu_res,

  output reg [63:0]     EXE_csr_res
);

wire [2:0] func3 =EXE_inst[14:12];
wire [6:0] opcode=EXE_inst[6:0];
always @(*) begin
    if(opcode==7'b1110011) begin
        case(func3)
        3'b001:                  //CSRRW
        begin
            EXE_csr_res = EXE_alu_res;
        end
        3'b010:                  //CSRRS
        begin
            EXE_csr_res = csr_val_exe | EXE_alu_res; //alu_res 为 rs1
        end
        3'b011:                  //CSRRC
        begin
            EXE_csr_res = ~csr_val_exe & EXE_alu_res;
        end
        3'b101:                  //CSRRWI
        begin
            EXE_csr_res = EXE_alu_res;
        end
        3'b110:                  //CSRRSI
        begin
            EXE_csr_res = csr_val_exe | csr_val_exe; //alu_res 为 imm
        end
        3'b111:                  //CSRRCI
        begin
            EXE_csr_res = ~csr_val_exe & EXE_alu_res;
        end
        default: EXE_csr_res = EXE_alu_res;
        endcase
    end
    else EXE_csr_res = EXE_alu_res;
end
endmodule